package tk.heimashuma.store.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.heimashuma.store.entity.Addrs;
import tk.heimashuma.store.entity.Transaction;
import tk.heimashuma.store.entity.Users;
import tk.heimashuma.store.repository.AddrRepository;
import tk.heimashuma.store.repository.TransactionRepository;
import tk.heimashuma.store.service.UserInfoService;

import java.util.List;
import java.util.Optional;

@Service
public class UserInfoServiceImpl implements UserInfoService {
    @Autowired
    private AddrRepository addrRepository;
    @Autowired
    private TransactionRepository transactionRepository;

    @Override
    public List<Addrs> findAddrByUser(Users user) {
        List<Addrs> addrsList = addrRepository.findAllByUser(user);
        // 电话号码处理
        for (int i = 0; i < addrsList.size(); i++) {
            Addrs addrs = addrsList.get(i);
            String iphone = addrs.getIphone();
            if(iphone.length() >= 11){
                String newIphone = iphone.replaceAll("^(.{3})(.*)(.{4})$", "$1*****$3");
                addrs.setIphone(newIphone);
            }
        }
        return addrsList;
    }

    @Override
    @Transactional
    public void addAddrByUser(Addrs addrs, Users users) {
        addrs.setUser(users);
        addrs = addrRepository.save(addrs);
    }

    @Override
    @Transactional
    public boolean delAddr(Integer aid,Users user) {
        Optional<Addrs> addrsOpt = addrRepository.findById(aid);
        if(addrsOpt.isPresent()){
            Addrs addrs = addrsOpt.get();
            if(addrs.getUser().getUid() == user.getUid()){
                addrRepository.deleteAddrById(aid);
                return true;
            }
        }
        return false;
    }

    @Override
    public List<Transaction> findTransactions(Integer page, Integer pageSize, Users user) {
        PageRequest pageRequest = PageRequest.of(page - 1, pageSize);
        Transaction transaction = new Transaction();
        transaction.setUser(user);
        Example<Transaction> ex = Example.of(transaction);
        Page<Transaction> all = transactionRepository.findAll(ex,pageRequest);
        List<Transaction> content = all.getContent();
        return content;
    }
    public List<Transaction> findConditionTransactions(Integer page, Integer pageSize, Users user,Integer state) {
        PageRequest pageRequest = PageRequest.of(page - 1, pageSize);
        Transaction transaction = new Transaction();
        transaction.setUser(user);
        transaction.setState(state);
        Example<Transaction> ex = Example.of(transaction);
        Page<Transaction> all = transactionRepository.findAll(ex,pageRequest);
        List<Transaction> content = all.getContent();
        return content;
    }

}
